package library.console;

import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import com.j256.ormlite.logger.LocalLog;

import library.*;

public class MainMenu {
	private Librarian loginLibrarian;
	private LibrarianController librarianController;
	private Scanner scanner;
	
	// System entry point
	public static void main(String args[]) throws SQLException {
		System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "ERROR");
		new MainMenu();
	}	
	//  Program start from here, do not wrint anything in main method
	public MainMenu() throws SQLException {
		try {
			DaoUtil.init();
			this.librarianController = new LibrarianController();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.print("Startup fail.");
			e.printStackTrace();
			return;
		}

        LibrarianController librarianController = new LibrarianController();
        try{
            librarianController.check("admin", "admin");
        } catch (Exception e) {
            System.out.println("No default user found. Creating default admin librarian....");
            Librarian adminLibrarian = new Librarian("admin", "admin");
            adminLibrarian.setAdminRight(true);
            librarianController.create(adminLibrarian);
        }
		scanner = new Scanner(System.in);
		if(login())
			showMainMenu();
	}
	
	public boolean login(){
		int loginErrorCount = 0;
		do{
			try {
				System.out.print("Enter Librarian login name: ");
				String userName = scanner.nextLine();
				System.out.print("Enter Password: ");
				String pwd = scanner.nextLine();
				loginLibrarian = librarianController.check(userName, pwd);
				return true;
			} catch (Exception ex){
				System.out.println(ex.getMessage() + " Please try again.");
				loginErrorCount ++;
			}
		} while (loginErrorCount < 3);	
		System.out.println("ERROR: Login failure over 3 times. Exiting.........");
		return false;
	}
	
	public void showMainMenu(){
		int input;
		do{
			input = -1;
			System.out.println("============= Main Menu==========");
			System.out.println("|  1: Administration            |");
			System.out.println("|  2: Borrow                    |");
			System.out.println("|  3: Return books              |");
			System.out.println("|  4: Quit                      |");
			System.out.println("=================================");
			
			try{
				input = scanner.nextInt();
			} catch (InputMismatchException misMatchEx){
				System.out.println("Invalid option, your input is not a number.");
				input = -1;
			} finally {
				scanner.nextLine();
			}
			
			switch (input) {
				case 1:
					if(loginLibrarian.isAdmin())
						new AdminMenu(scanner);
					else
						System.out.println("You don't have the permission to enter administration menu.");
					break;
				case 2:
					new BookingMenu(scanner);
					break;
				case 3:
					BookingMenu.returnBook(scanner);
					break;
				case 4:
					return;
				default:
					System.out.println("Invalid option, please enter a number between 1 to 4.");
					break;
			}
		} while(input != 4);
	}

}
